﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Heavy_Metals
{
    public class DrawCurves
    {
        //画坐标：originX,originY表示坐标原点在界面上的位置，lengthX，lengthY表示横纵坐标的长度，stepX，stepY表示横纵方向上的步长,stepXS,stepYS表示每个步长所代表的实际值
        public void DrawCoordinate(Graphics g, Pen p, int originX, int originY, int lengthX, int lengthY, int stepX, int stepY, double stepXS, double stepYS)
        {
            Font font1 = new Font("宋体", 6);
            p.Width = 2;
            int endY;//纵坐标上最上面一点的纵向位置
            int endX;//横坐标上最后一点的横向位置
            if (originY - lengthY < 0)
            {
                endY = 0;
            }
            else endY = originY - lengthY;
            endX = originX + lengthX;
            //画纵坐标线

            g.DrawLine(p, new Point(originX, originY), new Point(originX, endY));
            //画纵坐标刻度步长
            int tempY;
            tempY = originY;
            int i = 0;//统计步长数
            while (tempY >=(endY + stepY))
            {
                tempY = originY - (stepY * i);
                if (i == 0)
                {
                    g.DrawString("0", font1, Brushes.Black, new Point(originX - 5, originY + 5));//标刻度0
                }
                else
                {
                    g.DrawLine(p, new Point(originX -5, tempY), new Point(originX, tempY));//画刻度
                    p.Width = 1;
                    g.DrawLine(p, new Point(originX, tempY), new Point(endX, tempY));//画等值线
                    p.Width = 2;
                    g.DrawString((stepYS * i).ToString(), font1, Brushes.Black, new Point(originX - 20, tempY-5));//标刻度
                }
                i++;
            }
            //画横坐标线

            g.DrawLine(p, new Point(originX, originY), new Point(endX, originY));
            //画横坐标刻度步长
            int tempX;
            tempX = originX;
            int j = 0;
            while (tempX < endX)
            {
                tempX = originX + (stepX * j);
                if (j == 0) { }
                else
                {
                    g.DrawLine(p, new Point(tempX, originY + 5), new Point(tempX, originY));//画刻度
                    g.DrawString((stepXS * j).ToString(), font1, Brushes.Black, new Point(tempX-10, originY + 10));//标刻度
                }
                j++;
            }
        }
        //画曲线：String name表示曲线的名字，Point pName表示曲线名字的位置，int originX, int originY表示原点坐标， double perPixelY表示纵每个像素所代表的实际数值,stepX表示横纵方向上的步长。double[] data表示曲线数据
        public void DrawCurve(Graphics g, Pen p, String name, Point pName, int originX, int originY, double perPicelY, int stepX, double[] data)
        {
            Font font1 = new Font("宋体", 10);
            //标识线条的属性
            g.DrawString(name, font1, Brushes.Black, pName);
            Pen p1 = new Pen(p.Color, 3);
            g.DrawLine(p1, pName.X - 10, pName.Y, pName.X, pName.Y);
            //画曲线
            int tempX1;
            int tempY1;
            int tempX2;
            int tempY2;
            for (int i = 0; i < data.Length - 1; i++)
            {
                tempX1 = originX + i * stepX;
                tempY1 = (int)(originY - (data[i] / perPicelY));//计算起点的纵坐标

                tempX2 = tempX1 + stepX;
                tempY2 = (int)(originY - (data[i + 1] / perPicelY));//计算终点的坐标
                //绘制点所在的小圆圈
                Rectangle r = new Rectangle(tempX2 - 2, tempY2 - 2, 4, 4);
                g.DrawEllipse(p, r);
                //画两点间的线
                g.DrawLine(p, new Point(tempX1, tempY1), new Point(tempX2, tempY2));

            }
        }
    }
}
